const http = require('../../../utils/http.js');
Page({
  data: {
    pageTitle: '确认订单',
    payIcon: 1,
    onShowStatus: false,
    info: {
      address: ''
    },
    PageStatus: false,
    orderNumber: '',
    option: {}
  },
  onLoad: function (options) {
    var _this = this
    if (options.type) {
      _this.setData({
        PageStatus: true,
        orderNumber: options.order_num
      })
    } else {
      _this.setData({
        option: options
      })
    }
  },
  onReady: function () {},
  onShow: function () {
    var _this = this
    if (!_this.data.info.address) {
      _this.onPageInfo()
    }
  },
  onStatusBall() {
    wx.showToast({
      title: '账户余额不足',
      icon: 'none'
    })
  },
  shopImg() {
    var _this = this,
      detail = _this.data.info
    detail.goods.logo = 'https://huixuangou.oss-cn-beijing.aliyuncs.com/wechat/Xz_MiNi_image/detail360-360.png'
    _this.setData({
      info: detail
    })
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  onPageInfo() {
    var _this = this,
      PageStatus = _this.data.PageStatus,
      data = _this.data.option
      data.client_type = '3'
    if (!PageStatus) {
      http.getRequest('v1/settlement', data,
        function (res) {
          console.log(res)
          _this.setData({
            info: res.data,
            onBalanceStatus: Number(res.data.balance) > Number(res.data.actual_amount),
            payIcon: Number(res.data.balance) > Number(res.data.actual_amount) ? 1 : 0
          })
        },
        function (err) {
          wx.navigateTo({
            url: '/pages/Login/Index/index',
          })
        })
    } else {
      var option = _this.data.option
      http.getRequest('v1/getWaitPay', {
          order_num: _this.data.orderNumber,
          client_type:'3'
        },
        function (res) {
          console.log(res)
          option.buy_num = res.data.buy_num
          _this.setData({
            info: res.data,
            option: option,
            onBalanceStatus: Number(res.data.balance) > Number(res.data.actual_amount),
            payIcon: Number(res.data.balance) > Number(res.data.actual_amount) ? 1 : 0
          })
        },
        function (err) {
          wx.navigateTo({
            url: '/pages/Login/Index/index',
          })
        })
    }

  },
  upDataNumber(e) {
    var _this = this,
      id = e.currentTarget.id,
      info = _this.data.option,
      PageStatus = _this.data.PageStatus
    if (id == 1) {
      if (info.buy_num == 1) {
        wx.showToast({
          title: '不能再减了',
          icon: 'none'
        })
        return false
      }
      info.buy_num = (Number(info.buy_num) - 1)
      _this.setData({
        option: info
      })
      if (!PageStatus) {
        _this.onPageInfo()
      }
    } else {
      info.buy_num = (Number(info.buy_num) + 1)
      _this.setData({
        option: info
      })
      if (!PageStatus) {
        _this.onPageInfo()
      }
    }
  },
  onGotoaddress() {
    wx.navigateTo({
      url: '/pages/onNewPages/addressList/addressList',
    })
  },
  onStatusIcon(e) {
    var _this = this
    _this.setData({
      payIcon: e.currentTarget.id
    })
  },
  onPayButton() {
    var _this = this,
      member_address_id = _this.data.info.address ? _this.data.info.address.id : 999
    if (member_address_id == 999) {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none'
      })
      return false
    }
    _this.setData({
      onShowStatus: !_this.data.onShowStatus
    })
  },
  onPay(e) {
    var _this = this,
      data = {
        mall_sku_id: _this.data.info.goods.mall_sku_id,
        total_fee: _this.data.info.actual_amount,
        buy_num: _this.data.info.buy_num,
        member_address_id: _this.data.info.address ? _this.data.info.address.id : 999,
        pay_type: _this.data.payIcon == 0 ? 3 : 1
      },
      PageStatus = _this.data.PageStatus
    if (data.member_address_id == 999) {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none'
      })
      return false
    }
    http.postRequest(PageStatus ? 'v1/orderPayment' : 'v1/payment', PageStatus ? {
        order_num: _this.data.orderNumber,
        pay_type: _this.data.payIcon == 0 ? 3 : 1
      } : data,
      function (res) {
        console.log(res)
        if (_this.data.payIcon == 0) {
          wx.requestPayment({
            nonceStr: res.data.payment.nonceStr,
            package: res.data.payment.package,
            paySign: res.data.payment.paySign,
            timeStamp: res.data.payment.timeStamp,
            signType: 'MD5',
            success(rep) {
              wx.showToast({
                title: '支付成功',
                icon: 'success'
              })
              setTimeout(() => {
                http.postRequest('v1/callback', {
                    order_num: res.data.order_num
                  },
                  function (res) {
                    console.log(res)
                    if (res.code == 201) {
                      http.postRequest('syncData', {
                          is_sync: 1
                        },
                        function (res) {
                          wx.setStorageSync('member', res.data)
                          getApp().globalData.token = res.data.token;
                          if (res.code == 200) {
                            console.log(res)
                            if (res.data.member_info != undefined) {
                              wx.setStorageSync('renzheng', 1)
                            }
                          }
                        },
                        function (res) {})
                    }
                    wx.setStorageSync('paymentInfo', res.data.goods)
                    wx.redirectTo({
                      url: '/pages/onNewPages/orderSuccess/orderSuccess',
                    })
                    setTimeout(() => {
                      _this.setData({
                        onShowStatus: !_this.data.onShowStatus
                      })
                    }, 500);
                  },
                  function (err) {})
              }, 1000);
            },
            fail(res) {
              wx.showToast({
                title: '支付失败',
                image: 'https://huixuangou.oss-cn-beijing.aliyuncs.com/wechat/Xz_MiNi_image/icon_toast_defeat@2x.png',
              })
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/onNewPages/myOrder/myOrder?type=0',
                })
              }, 1200);
            }
          })
        } else {
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          })
          setTimeout(() => {
            http.postRequest('v1/callback', {
                order_num: res.data.order_num
              },
              function (res) {
                console.log(res)
                if (res.code == 201) {
                  http.postRequest('syncData', {
                      is_sync: 1
                    },
                    function (res) {
                      wx.setStorageSync('member', res.data)
                      getApp().globalData.token = res.data.token;
                      if (res.code == 200) {
                        console.log(res)
                        if (res.data.member_info != undefined) {
                          wx.setStorageSync('renzheng', 1)
                        }
                      }
                    },
                    function (res) {})
                }
                wx.setStorageSync('paymentInfo', res.data.goods)
                wx.redirectTo({
                  url: '/pages/onNewPages/orderSuccess/orderSuccess',
                })
                setTimeout(() => {
                  _this.setData({
                    onShowStatus: !_this.data.onShowStatus
                  })
                }, 500);
              },
              function (err) {})
          }, 1000);
        }
      },
      function (err) {
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
  }
})